package com.jms.web.controller.jms.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.jms.common.core.domain.entity.SysDept;
import com.jms.common.core.domain.model.LoginUser;
import com.jms.common.filter.utils.SecurityUtils;
import com.jms.common.filter.utils.poi.ExcelUtil;
import com.jms.system.service.ISysDeptService;
import com.jms.system.service.impl.SysDeptServiceImpl;
import com.jms.web.controller.jms.domain.Question;
import com.jms.web.controller.jms.service.IQuestionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jms.common.annotation.Log;
import com.jms.common.core.controller.BaseController;
import com.jms.common.core.domain.AjaxResult;
import com.jms.common.enums.BusinessType;
import com.jms.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 问题反映Controller
 * 
 * @author drx
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/system/question")
public class QuestionController extends BaseController
{
    @Resource
    private IQuestionService questionService;

    @Resource
    private ISysDeptService sysDeptService;

    /**
     * 查询问题反映列表
     */
    @PreAuthorize("@ss.hasPermi('system:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(Question question)
    {

        /*
        * 获取当前用户部门区划编码
        * */
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        String areaShortCode = sysDept.getAreaShortCode();

        areaShortCode = areaShortCode.replaceAll("0+$", "").replaceAll("\\.$", "");

        question.setAreaShortCode(areaShortCode);

        startPage();
        List<Question> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }


    /**
     * 查询问题反映列表
     */
    @PreAuthorize("@ss.hasPermi('system:question:list')")
    @PostMapping("/getList")
    public TableDataInfo getList(@RequestBody Question question)
    {

        /*
         * 获取当前用户部门区划编码
         * */
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        String areaShortCode = sysDept.getAreaShortCode();

        areaShortCode = areaShortCode.replaceAll("0+$", "").replaceAll("\\.$", "");

        question.setAreaShortCode(areaShortCode);

        startPage();
        List<Question> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }


    /**
     * 导出问题反映列表
     */
    @PreAuthorize("@ss.hasPermi('system:question:export')")
    @Log(title = "问题反映", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Question question)
    {
        List<Question> list = questionService.selectQuestionList(question);
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, list, "问题反映数据");
    }

    /**
     * 获取问题反映详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(questionService.selectQuestionById(id));
    }

    /**
     * 新增问题反映
     */
    @PreAuthorize("@ss.hasPermi('system:question:add')")
    @Log(title = "问题反映", businessType = BusinessType.INSERT)
    @PostMapping(value = "add")
    public AjaxResult add(@RequestBody Question question)
    {
        return toAjax(questionService.insertQuestion(question));
    }

    /**
     * 修改问题反映
     */
    @PreAuthorize("@ss.hasPermi('system:question:edit')")
    @Log(title = "问题反映", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Question question)
    {
        return toAjax(questionService.updateQuestion(question));
    }


    /**
     * 修改问题反映
     */
    @PreAuthorize("@ss.hasPermi('system:question:edit')")
    @Log(title = "问题反映", businessType = BusinessType.UPDATE)
    @PostMapping("/editQuestion")
    public AjaxResult editQuestion(@RequestBody Question question)
    {
        return toAjax(questionService.editQuestion(question));
    }


    /**
     * 删除问题反映
     */
    @PreAuthorize("@ss.hasPermi('system:question:remove')")
    @Log(title = "问题反映", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(questionService.deleteQuestionByIds(ids));
    }
}
